/// Estimate elasticities of demand

********************************************************************************
*** load data
use "data/pseudodata/userlevel_pseudodata.dta", clear

*replace bound 1 to 100
replace bound = 100 if bound == 1
replace threemonther = 100 if threemonther == 1

* Transform variables 

*dayrate
gen lndayrate = log(dayrate)
gen ihsdayrate = log(dayrate + sqrt(dayrate^2 + 1))
gen ln1dayrate = log(dayrate+1)

*days insured
gen lndaysins = log(totdayscoverage90)
gen ihsdaysins = log(totdayscoverage90 + sqrt(totdayscoverage90^2 + 1))
gen ln1daysins = log(totdayscoverage90 + 1)

*base daily premium
gen lnbasedayrate = log(basedayrate)
gen ihsbasedayrate = log(basedayrate + sqrt(basedayrate^2 + 1))
gen ln1basedayrate = log(basedayrate + 1)

***alternative bound (more than 3 days)
gen bound3 = bound
replace bound3 = 0 if paydays1 == 3

replace bound = 100 if bound ==1
replace bound3= 100 if bound3==1

***label variables
label variable lndayrate "\textbf{Log(Daily premium)}"
label variable lnbasedayrate "Log(Base daily premium)"
label variable lndaysins "Log(Days Insured)"
label variable ihsdayrate "\textbf{asinh(Daily Premium)}"
label variable ihsbasedayrate "asinh(Base Daily Premium)"
label variable ln1dayrate "\textbf{log(Daily Premium + 1)}"
label variable ln1basedayrate "log(Base Daily Premium + 1)"
label variable ihsdaysins "asinh(Days insured)"
label variable bundle "Bundle discount offered"


********************************************************************************
*** elasticity of demand for enrollment

eststo: reg bound lndayrate lnbasedayrate bundle if daily == 1, r
estadd local samp "All"
eststo: reg bound3 lndayrate lnbasedayrate bundle if daily == 1, r
estadd local samp "All"
eststo: reg ihsdaysins lndayrate lnbasedayrate bundle if daily==1, r
estadd local samp "All"
eststo: reg ihsdaysins lndayrate lnbasedayrate bundle if daily==1 & bound==100, r
estadd local samp "Enrolled"

cd "${root}"

esttab using "output/tables/enroll demand elasticity.tex", ///
	replace tex label gaps nomtitle eqlabels("\vspace{-1.5ex}") ///
	stats(N samp, fmt(%11.0gc) labels("N" "Sample")) ///
	cells(b(fmt(2) label("\vspace{-.5ex}\phantom{")) se(par fmt(2) label(" ")) ///
		p(par([ ]) fmt(3) label("}"))) ///
	mgroups("\vspace{-.5ex}Take-up" "Take-up, $>3$" "asinh(Days insured)", ///
		pattern(1 1 1 0) ///
		prefix(\multicolumn{@span}{c}{) suffix(}) span erepeat(\cmidrule(lr){@span}))		
eststo clear

********************************************************************************
*** additional transforms

eststo clear

local scale = 1

quietly eststo: reg lndaysins lndayrate lnbasedayrate bundle if daily == 1, r
estadd local samp "\scalebox{`scale'}{Yes}"
quietly eststo: reg lndaysins ihsdayrate ihsbasedayrate bundle if daily == 1 & bound == 100, r
estadd local samp "\scalebox{`scale'}{Yes}"
quietly eststo: reg lndaysins ln1dayrate ln1basedayrate bundle if daily == 1 & bound == 100, r
estadd local samp "\scalebox{`scale'}{Yes}"
quietly eststo: reg ihsdaysins ihsdayrate ihsbasedayrate bundle if daily == 1 & bound == 100, r
estadd local samp "\scalebox{`scale'}{Yes}"
quietly eststo: reg ihsdaysins ihsdayrate ihsbasedayrate bundle if daily == 1, r
estadd local samp "\scalebox{`scale'}{No}"
quietly eststo: reg ln1daysins ln1dayrate ln1basedayrate bundle if daily == 1 & bound == 100, r
estadd local samp "\scalebox{`scale'}{Yes}"
quietly eststo: reg ln1daysins ln1dayrate ln1basedayrate bundle if daily == 1, r
estadd local samp "\scalebox{`scale'}{No}"


esttab using "output/tables/elasticity transforms.tex", ///
	label replace gaps nomtitle ///
	stats(N, fmt(%11.3gc) labels("N")) ///
	cells(b(fmt(2) label("\vspace{-.4ex}\phantom{")) se(par fmt(2) label(" ")) ///
		p(par([ ]) fmt(3) label("}"))) ///
	mgroups("\vspace{-.2ex}log(Days Insured)" "asinh(Days Insured)" ///
			"log(Days Insured + 1)", ///
		pattern(1 0 0 1 0 1 0) ///
		prefix(\multicolumn{@span}{c}{) suffix(}) span erepeat(\cmidrule(lr){@span})) ///
	order(lndayr* lnb* ihs* ln1* bundle)
		
